package com.cloudptt.wp;

import android.telephony.CellIdentityGsm;
import android.telephony.CellIdentityLte;
import android.telephony.CellInfo;
import android.telephony.CellInfoGsm;
import android.telephony.CellInfoLte;
import android.telephony.CellLocation;
import android.telephony.PhoneStateListener;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;

import com.cloudptt.api.product.log.Log;

import java.util.List;

public class AppPhoneStateListener extends PhoneStateListener {
    public int getCellId() {
        return cellId;
    }

    private int cellId = 0;
    @Override
    public void onCellLocationChanged(CellLocation location) {
        if(location == null)
        {
            return;
        }
        if (location instanceof GsmCellLocation) {  // GSM 网络
            GsmCellLocation gsmLocation = (GsmCellLocation) location;
           cellId = gsmLocation.getCid();       // 获取小区 ID‌:ml-citation{ref="1,3" data="citationList"}
            int lac = gsmLocation.getLac();         // 获取位置区码（LAC）
        } else if (location instanceof CdmaCellLocation) {  // CDMA 网络
            CdmaCellLocation cdmaLocation = (CdmaCellLocation) location;
            cellId = cdmaLocation.getBaseStationId();
        }
        Log.debug(this.getClass(),"onCellLocationChanged cellId = " + cellId);
    }

    // Android 7.0+ 推荐使用 onCellInfoChanged
    @Override
    public void onCellInfoChanged(List<CellInfo> cellInfo) {
        if(cellInfo == null)
        {
            return;
        }
        for (CellInfo info : cellInfo) {
            if (info instanceof CellInfoGsm) {
                CellIdentityGsm identity = ((CellInfoGsm) info).getCellIdentity();
                cellId = identity.getCid();
            } else if (info instanceof CellInfoLte) {
                CellIdentityLte identity = ((CellInfoLte) info).getCellIdentity();
                cellId = identity.getCi();
            }
        }
        Log.debug(this.getClass(),"onCellInfoChanged cellId = " + cellId);
    }
}
